// 因为actions里面要请求数据，因此引入API
import { getUserInfoAPI } from "../../request/api"

export default {
    // 命名空间模块
    namespaced: true,
    state: {
        cartTotal: 0,
        userInfo: {
            nickName: "昵称",
            coin: "--",
            headImg: require('../../assets/img/userImg.f8bbec5e.png')
        }
    },
    mutations: {
        // 修改用户信息
        updateUserInfo(state, payload) {
            state.cartTotal = payload.cartTotal
            state.userInfo = payload.userInfo
        },
        // 初始化用户信息
        initUserInfo(state) {
            state.cartTotal = 0
            state.userInfo = {
                nickName: "昵称",
                coin: "--",
                headImg: require('../../assets/img/userImg.f8bbec5e.png')
            }
        }

    },
    actions: {
        // 请求用户信息，并触发mutations里面的修改用户信息方法，将渲染的数据修改为请求回来的数据
        async asyncUpdateUserInfo(context, payload) {
            let res = await getUserInfoAPI();
            context.commit("updateUserInfo", res.data)
        }
    }
}